package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.NewsDao;
import com.hlingsoft.cms.dao.ReportDao;
import com.hlingsoft.cms.entity.News;
import com.hlingsoft.cms.entity.Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * Created by selbylei on 2015/5/28.
 */
@Controller
@RequestMapping("report")
public class ReportAction extends BaseAction {
    @Autowired
    private ReportDao reportDao;

    @Autowired
    private NewsDao newsDao;

    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "newsId") long newsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();
            News news = newsDao.getNewsById(newsId);
            Report report = new Report();
            report.setMyId(myId);
            report.setNewsId(newsId);
            report.setToId(news.getId());
            report.setCreateTime(now);
            report.setUpdateTime(now);

            reportDao.addReport(report);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
